<?php
include 'facebook.php';
include 'appinclude.php';

// set up facebook api
$facebook = new Facebook($GLOBALS["api_key"], $GLOBALS["api_secret"]);
$facebook->require_frame();
$user = $facebook->require_login();

// establish link with mysql database
$link = @mysql_connect($GLOBALS["db_host"], $GLOBALS["db_user"], $GLOBALS["db_pass"]);
@mysql_select_db($GLOBALS["db_name"]);

// check if calendar in db
$sem = $GLOBALS["semester"];
$cal_res = mysql_query("SELECT cal_string from cal_strings WHERE uid=$user AND semester='$sem'");
?>


<?php if(mysql_num_rows($cal_res) == 0): ?>
<fb:redirect url="<?php echo($GLOBALS["full_canvas"]);?>/index.php" />
<?php else: ?>

<fb:header>ScheduleMatch</fb:header>
<fb:tabs>
    <fb:tab-item href='index.php' title='Home' selected='false'/>
    <fb:tab-item href='classmates.php' title='Classmates' selected='false'/>
    <fb:tab-item href='settings.php' title='Settings' selected='true'/>
    <fb:tab-item href='invite.php' title='Invite Friends' selected='false'/>
</fb:tabs>

<br />
<div align="center">
<form id = "change_settings" method = "post" action="addschedule.php">
<?php if($_GET['invalid'] == 1): ?>
This schedule is invalid, please try again.<br /><br />
<?php else: ?>
New calendar URL? Enter it below.<br /><br />
<?php endif; ?>
<input type="text" name="cal_url" id="cal_url" value="<?php echo('http://scheduleman.org/schedules/'.get_cal_string($user))?>" />
<input type="hidden" name="cal_change" value="true" />
<select name="sem" id="sem">
  <option value="S10" selected="selected">Spring 2010</option>
  <option value="F09">Fall 2009</option>
</select>
<input type="submit" value="Submit" class="inputsubmit"/> 
</form>
<div>
<br />
<div>

<?php endif; ?>
<?php mysql_close($link); ?>